package helpers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import entities.Article;
import entities.AutoSite;

public class CrawlerSiteHelper {
	
	public static List<String> getLastestArticle(AutoSite autoSite) {
		ArrayList<String> listArticles = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(autoSite.getUrl()).get();
			Elements links = doc.select(autoSite.getRegLink());
			if (links.size() > 0) {
				for (int i = 0; i < links.size(); i++) {
					if(!Pattern.matches("^http://[\\w\\W]+$",links.get(i).attr("href")))
					{
						listArticles.add("http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + links.get(i).attr("href"));
					} else {
						listArticles.add(links.get(i).attr("href"));
					}
				}
			}
			
		} catch (Exception e) {
			listArticles = new ArrayList<String>();
			e.printStackTrace();
		}
		return listArticles;
	}
	
	public static Article getArticle(String url, AutoSite autoSite) {
		Article article = new Article();
		try {
			Document doc = Jsoup.connect(url).get();
			String title = doc.select(autoSite.getRegTitle()).text();
			if (autoSite.isTranslate()) {
				title = TranslateHelper.translate(title, autoSite.getLanguage(), autoSite.getTranslate());
			}
			
			String alias = StringHelper.replace(title);
			
			String author = autoSite.getCreator();
			if (!autoSite.getRegAuthor().isEmpty()) {
				author = doc.select(autoSite.getRegAuthor()).text();
			}
			
			if (!autoSite.getRegRemove().isEmpty()) {
				doc.select(autoSite.getRegRemove()).remove();
			}
			
			String content = doc.select(autoSite.getRegContent()).html();
			if (autoSite.isTranslate()) {
				content = TranslateHelper.translate(doc.select(autoSite.getRegContent()).text(), autoSite.getLanguage(), autoSite.getTranslate());
			}
			
			
			//Get image storage google and read image
			Elements imgs = doc.select(autoSite.getRegContent()).select("img");
			for (int i = 0; i < imgs.size(); i++) {
				
				String src = imgs.get(i).attr("src");
				if(!Pattern.matches("^(http|https)://[\\w\\W]+$",src))
				{
					//src = "http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + src;
					content = content.replace(src, "http://"+UrlHelper.fomatDomains(autoSite.getUrl()) + src);
				}
				
				/*String name = UrlHelper.getMainDomain(src);
				GoogleStorageHelper.getFile(src, name);
				description = description.replace(src, "http://static.services79.com/ats100/" + name);*/
			}
			
			
			article.setId(IdUniqueHelper.getId());
			article.setUrl(url);
			article.setTitle(title);
			article.setAlias(alias);
			article.setContent(content);
			article.setAuthorName(author);
			article.setCategory(autoSite.getCategory());
			article.setAutoSite(autoSite.getId());
			article.setAuthKey(IdUniqueHelper.getId());
			article.setStatus(autoSite.getStatus());
			
		} catch (IOException e) {
			article = null;
			e.printStackTrace();
		}
		
		return article;
	}
	
}
